﻿define(["angularAMD"], function (angularAMD) {
    angularAMD.service('B0251Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            var machine_no;
            this.init = function (options) {
                machine_no = options.machine_no;
                $mdDialog.dialog(
                'module/moduleB/program/B02/B0251/B0251.html', function (dialog) {
                    return {
                        css_url: 'module/moduleB/program/B02/B0251/B0251.css',
                        workLotNo: '',
                        ASData: '',
                        back: function () {
                            dialog.hide();
                        },                            
                        confirm: function () {
                            
                        },
                        searchNextlotno: function () {
                            $rootScope.showQueryList({
                                header: [
                                    { label: $filter('translate')('B0251.LotNo'), code: 'LotNo' },
                                    { label: $filter('translate')('B0251.EquipmentNo'), code: 'EquipmentNo' },
                                    { label: $filter('translate')('B0251.status'), code: 'status' },
                                    { label: $filter('translate')('B0251.OPNo'), code: 'OPNo' },
                                    { label: $filter('translate')('B0251.workdate'), code: 'workdate' },
                                    { label: $filter('translate')('B0251.seq'), code: 'seq' }                                    
                                ],
                                query: function (conditions, feedback) {
                                    //NCCode_info_get(feedback, dataObject.plot_no, dataObject.op_no, machine_no);
                                    next_lotno_with_equipmentno_info_get(feedback);
                                },
                                confirm: function (selectItem, query_dialog) {
                                    if (selectItem.LotNo != null) {                                        
                                        dialog.workLotNo = selectItem.LotNo;
                                        $MMWService.sendToServer({
                                            uri: 'ServicesSTD.Module_WIP.accessory_state_info_get',
                                            content: {
                                                machine_no: options.machine_no,
                                                lot_no: dialog.workLotNo
                                            },
                                            success: function (data) {
                                                dialog.ASData = data;
                                            }
                                        });
                                    }
                                    query_dialog.hide();
                                }
                            });
                        },
                        beforeShown: function () {
                            $MMWService.sendToServer({
                                uri: 'ServicesSTD.Module_WIP.accessory_state_and_next_lotno_info_get',
                                content: {
                                    machine_no: options.machine_no
                                },
                                success: function (data) {
                                    dialog.workLotNo = data.workLotNo;
                                    dialog.ASData = data.ASData;
                                }
                            });
                        }
                    };
                });                
            };

            function next_lotno_with_equipmentno_info_get(callback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.next_lotno_with_equipmentno_info_get',
                    content: {
                        machine_no: machine_no
                    },
                    success: function (data) {
                        if (callback)
                            callback(data);
                    }
                });
            }
        }]);
});
